﻿using System.Collections;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;


namespace Fei
{

    public class TouchEvent : MonoBehaviour
    {
        // Start is called before the first frame update
        public Text answerText;
        private string correctAnswer;
        public Poetry.Questions _question;//问题管理器Question控件
        public ShowQuestion _showQuestion;//问题展示器Question控件

        void Start()
        {
            correctAnswer = _question.tmpQuestion.GetAnswer(); //已经换成湘男给的数据
            for (int i = 0; i < transform.childCount; i++)
            {
                transform.GetChild(i).GetComponent<Button>().onClick.AddListener(call: OnButtonClick);
            }
        }


        public void OnButtonClick()
        {
            //按钮被按下
            Text buttonText = EventSystem.current.currentSelectedGameObject.GetComponentInChildren<Text>();

            if (answerText.text.Length < 12) //字别太多了
            {
                answerText.text += buttonText.text;
            }

            if (answerText.text == correctAnswer)
            {
                Invoke("AnswerCorrect", 1.0f); //1s后再清空
            }
        }


        //更新下一题
        public void AnswerCorrect()
        {
            //回答正确
            ProgressCtrl.completeQuestion = true;
            answerText.text = "";
            _question.tmpQuestion = _question.tmpQuestionBox.GetQuestion();///拿出一个新的问题

            //更新成下一题，到时候换成湘男给的数据
            ShowQuestion.questionText = _question.tmpQuestion.GetIssue();
            for (int i = 0; i < ShowQuestion.optionsText.Length;i++) //因为一边是string，一边是char，技术人员害怕改一点崩全部，决定在这里依次进行类型转换
            {
                ShowQuestion.optionsText[i] = _question.tmpQuestion.GetChoices()[i][0];
            }
            //ShowQuestion.optionsText = new char[] { '雪', '风', '真', '派', '湘', '飞', '月', '欤', '对', '柳', '由', '怡' };
            correctAnswer = _question.tmpQuestion.GetAnswer();

            //更换题目和选项
            _showQuestion.question.text = ShowQuestion.questionText;
            for (int i = 0; i < 12; i++)
            {
                _showQuestion.option[i].text = ShowQuestion.optionsText[i].ToString();//（前接TouchEvent.cs  64行）后来技术人员发现又被转化回来了。
            }

        }
    }
}